package org.example.classtopia.Controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.example.classtopia.Common.Result.Result;
import org.example.classtopia.Mapper.UserMapper;
import org.example.classtopia.Model.Entity.Course;
import org.example.classtopia.Model.Entity.User;
import org.example.classtopia.Model.dto.AiSessionCreateDTO;
import org.example.classtopia.Model.vo.AiSessionVO;
import org.example.classtopia.Service.AiChatSessionHistoryService;
import org.example.classtopia.Service.AiCourseAdviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.List;

@Slf4j
@RestController
@Tag(name = "AI课程学习建议")
@RequestMapping("/aiAdvice")
public class AiCourseAdviceController {

    @Autowired
    private AiCourseAdviceService aiCourseAdviceService;

    @Autowired
    private AiChatSessionHistoryService aiChatSessionHistoryService;

    @Autowired
    private UserMapper userMapper;

    @Operation(description = "获取课程信息")
    @PostMapping("/getCourse")
    public List<Course> GetCourse(@RequestParam("用户Id") Long userId,
                                  @RequestParam(name = "学期开始日期", defaultValue = "2025-02-17")
                                  String semester) {
        return aiCourseAdviceService.GetAllCourse(userId, semester);
    }

    @Operation(description = "获取关键词")
    @PostMapping("/getPrompt")
    public String getprompt(@RequestParam("用户Id") Long userId,
                            @RequestParam(name = "学期开始日期", defaultValue = "2025-02-17")
                            String semester) {
        List<Course> courses = aiCourseAdviceService.GetAllCourse(userId, semester);
        return aiCourseAdviceService.GetPrompt(courses);
    }

    @Operation(description = "AI生成学习计划")
    @PostMapping("/aiGenerate")
    public Result<?> aiGenerate(
                                @RequestParam(value = "学期开始日期", defaultValue = "2025-02-17") String semester,
                                @RequestParam("用户id") Long userId,
                                @RequestParam(value = "sessionId") String sessionId){
        List<Course> courses = aiCourseAdviceService.GetAllCourse(userId, semester);

        User user = userMapper.FindDataByID(Math.toIntExact(userId));
        if(user == null){
            return Result.error("用户不存在");
        }

        String userReply = "能告诉我告诉我这个学期的学习计划安排吗？";

        if(courses.isEmpty()){

            String prompt = "复述“对不起，您这个学期还没有课程哦~”这句话，其他一律不需要";

            return aiChatSessionHistoryService.AIgetChatResponse(prompt, sessionId, user, userReply);
        }

        String prompt = aiCourseAdviceService.GetPrompt(courses);

        return aiChatSessionHistoryService.AIgetChatResponse(prompt, sessionId, user, userReply);
    }
}
